package handler

import (
	"context"
	"gozero-microservices/gateway-api/internal/logic"
	"gozero-microservices/gateway-api/internal/types"
	"gozero-microservices/gateway-api/utils"
	"net/http"
	"strconv"

	"github.com/zeromicro/go-zero/rest/httpx"
)

/*
 +----------------------------------------------------------------------
 + Title        : BannerHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 横幅处理器
 +----------------------------------------------------------------------
*/

type BannerHandler struct {
	BaseHandler
}

/**
 * 新增
 *
 * @param string Title ------------------------必传,标题
 * @param string ImageUrl ---------------------非必传,图片
 * @param uint8 Type --------------------------非必传,类型
 * @param string Link -------------------------非必传,链接
 * @param uint8 Sort --------------------------非必传,排序
 * @param string Note -------------------------非必传,备注
 * @return json
 * @author huwl
 * @date 2025/10/20 18:51:38
 */
func (control *BannerHandler) Add(w http.ResponseWriter, r *http.Request) {
	var req types.BannerAddRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Add(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("新增横幅成功"))
}

/**
 * 删除
 *
 * @param string Ids --------------------------必传,Id支持多个
 * @return json
 * @author huwl
 * @date 2025/10/20 18:51:38
 */
func (control *BannerHandler) Delete(w http.ResponseWriter, r *http.Request) {
	var req types.BannerDeleteRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Delete(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("删除横幅成功"))
}

/**
 * 编辑
 *
 * @param uint16 id ---------------------------必传,Id
 * @param string title ------------------------非必传,标题
 * @param string image_url --------------------非必传,图片
 * @param uint8 type --------------------------非必传,类型
 * @param string link -------------------------非必传,链接
 * @param string note -------------------------非必传,备注
 * @param uint8 sort --------------------------非必传,排序
 * @return json
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (control *BannerHandler) Edit(w http.ResponseWriter, r *http.Request) {
	var req types.BannerEditRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Edit(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("编辑横幅成功"))
}

/**
 * 编辑状态
 *
 * @param string ids --------------------------必传,Id支持多个
 * @param int status --------------------------必传,状态
 * @return json
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (control *BannerHandler) EditStatus(w http.ResponseWriter, r *http.Request) {
	var req types.BannerEditStatusRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	result, err := bannerLogic.EditStatus(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage(result.Msg))
}

/**
 * 详情
 *
 * @param int id ------------------------------必传,Id
 * @return json
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (control *BannerHandler) Detail(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.Atoi(utils.DefaultQuery(r, "id", "0"))

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	result, err := bannerLogic.Detail(uint16(id))
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(result))
}

/**
 * 列表
 *
 * @param int Id ------------------------------非必传,Id
 * @param string Ids --------------------------非必传,Id支持多个
 * @param string Title ------------------------非必传,标题
 * @param string Type -------------------------非必传,类型
 * @param string Status -----------------------非必传,状态
 * @param string StartCreateTime --------------非必传,开始创建时间
 * @param string EndCreateTime ----------------非必传,结束创建时间
 * @param string SortField --------------------非必传,排序字段
 * @param string SortType ---------------------非必传,排序方式
 * @param int PageSize ------------------------非必传,条数
 * @param int Page ----------------------------非必传,页码
 * @return json
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (control *BannerHandler) List(w http.ResponseWriter, r *http.Request) {
	var req types.BannerQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	result, err := bannerLogic.List(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"page":       result.Page,
		"page_size":  result.PageSize,
		"total":      result.Total,
		"page_total": result.PageTotal,
		"banners":    result.Banners,
	}))
}

/**
 * 数量
 *
 * @param int Id ------------------------------非必传,Id
 * @param string Ids --------------------------非必传,Id支持多个
 * @param string Title ------------------------非必传,标题
 * @param string Type -------------------------非必传,类型
 * @param string Status -----------------------非必传,状态
 * @param string StartCreateTime --------------非必传,开始创建时间
 * @param string EndCreateTime ----------------非必传,结束创建时间
 * @return json
 * @author huwl
 * @date 2025/10/22 10:15:55
 */
func (control *BannerHandler) Total(w http.ResponseWriter, r *http.Request) {
	var req types.BannerQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	result, err := bannerLogic.Total(req)
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"total": result.Total,
	}))
}
